package com.ruoyi.project.system.setting.controller;

import java.util.List;
import java.util.UUID;

import com.ruoyi.common.utils.security.ShiroUtils;
import com.ruoyi.project.system.setting.service.ITbParamSettingService;
import com.ruoyi.project.system.user.domain.User;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.project.system.setting.domain.TbParamSetting;

import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.web.page.TableDataInfo;

/**
 * 基本参数设置Controller
 * 
 * @author kangjie
 * @date 2019-12-28
 */
@Controller
@RequestMapping("/system/setting")
public class TbParamSettingController extends BaseController
{
    private String prefix = "system/setting";

    @Autowired
    private ITbParamSettingService tbParamSettingService;

    @RequiresPermissions("system:setting:view")
    @GetMapping()
    public String setting()
    {
        return  "system/setting/setting";
    }

    /**
     * 查询基本参数设置列表
     */
    @RequiresPermissions("system:setting:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(TbParamSetting tbParamSetting)
    {
        startPage();
        List<TbParamSetting> list = tbParamSettingService.selectTbParamSettingList(tbParamSetting);
        return getDataTable(list);
    }

    /**
     * 导出基本参数设置列表
     */
    @RequiresPermissions("system:setting:export")
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(TbParamSetting tbParamSetting)
    {
        List<TbParamSetting> list = tbParamSettingService.selectTbParamSettingList(tbParamSetting);
        ExcelUtil<TbParamSetting> util = new ExcelUtil<TbParamSetting>(TbParamSetting.class);
        return util.exportExcel(list, "setting");
    }

    /**
     * 新增基本参数设置
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存基本参数设置
     */
    @RequiresPermissions("system:setting:add")
    @Log(title = "基本参数设置", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(TbParamSetting tbParamSetting)
    {
        tbParamSetting.setId(UUID.randomUUID().toString().replaceAll("-",""));
        tbParamSetting.setCreateBy(ShiroUtils.getLoginName());
        tbParamSetting.setUpdateBy(ShiroUtils.getLoginName());
        return toAjax(tbParamSettingService.insertTbParamSetting(tbParamSetting));
    }

    /**
     * 修改基本参数设置
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") String id, ModelMap mmap)
    {
        TbParamSetting tbParamSetting = tbParamSettingService.selectTbParamSettingById(id);
        mmap.put("tbParamSetting", tbParamSetting);
        return prefix + "/edit";
    }

    /**
     * 修改保存基本参数设置
     */
    @RequiresPermissions("system:setting:edit")
    @Log(title = "基本参数设置", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(TbParamSetting tbParamSetting)
    {
        tbParamSetting.setCreateBy(ShiroUtils.getLoginName());
        tbParamSetting.setUpdateBy(ShiroUtils.getLoginName());
        return toAjax(tbParamSettingService.updateTbParamSetting(tbParamSetting));
    }

    /**
     * 删除基本参数设置
     */
    @RequiresPermissions("system:setting:remove")
    @Log(title = "基本参数设置", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(tbParamSettingService.deleteTbParamSettingByIds(ids));
    }

    /**
     * 数据状态修改
     */
    @Log(title = "基本参数设置", businessType = BusinessType.UPDATE)
    @RequiresPermissions("system:setting:edit")
    @PostMapping("/changeStatus")
    @ResponseBody
    public AjaxResult changeStatus(TbParamSetting tbParamSetting)
    {
        Subject sub = SecurityUtils.getSubject();
        User obj = (User)sub.getPrincipal();
        tbParamSetting.setUpdateBy(obj.getLoginName());
        int num = tbParamSettingService.updateFlag(tbParamSetting);
        tbParamSettingService.checkFlag();
        return toAjax(num);
    }


}
